/**
 *      Attribute:
 *          (
 *              id int primary key AUTOINCREMENT,
 *              categoryId int not null,                //种类id
 *              name string not null,                   //应用名
 *              thumbnail text,                         //缩略图
 *              install text,                           //安装路径
 *              deployed int                            //是否已部署
 *          )
 */
const { Sequelize, DataTypes } = require('sequelize')
const express = require('express')

function createModel(sequelize) {
    return sequelize.define('application', {
        id: { type: DataTypes.INTEGER, primaryKey: true },
        categoryId: { type: DataTypes.INTEGER, allowNull: false },
        name: { type: DataTypes.STRING, allowNull: false },
        thumbnail: {type: DataTypes.STRING },
        deployed: { type: DataTypes.BOOLEAN, defaultValue: false }
    }, {
        freezeTableName: true,
        tableName: 'application',
        timestamps: false
     })
}

let router = express.Router()
let appModel = undefined

/**
 * @swagger
 * /api/v2/app:
 *      get:
 *          tags: [Application]
 *          summary:
 *          description: 获取应用信息
 *          parameters:
 *              - name: id
 *                in: query
 *                required: false
 *                type: string
 *              - name: categoryId
 *                in: query
 *                required: false
 *                type: string
 *          responses:
 *              200:
 *                  description: successed
 *                  content:
 *                      application/json:
 *                          schema:
 *                              type: object
 *                              properties:
 *                                  result:
 *                                      type: string
 *                                  desc:
 *                                      type: string
 *                                  data:
 *                                      type: array
 *                                      items:
 *                                          type: object
 *                                          properties:
 *                                              id:
 *                                                  type: string
 *                                              categoryId:
 *                                                  type: string
 *                                              name:
 *                                                  type: string
 *                                              thumbnail:
 *                                                  type: string
 *                                              deployed:
 *                                                  type: integer
 *                                              
 *              500:
 *                  description: failed
 */
router.get('/app', (req, res)=>{
    let condition = { where:{
            id: req.query.id,
            categoryId: req.query.categoryId
        }}
    if(condition.where.id == undefined || condition.where.id == '') {
        delete condition.where.id
    }
    if(condition.where.categoryId == undefined || condition.where.categoryId == '') {
        delete condition.where.categoryId
    }
    if(Object.keys(condition.where).length == 0) {
        delete condition.where
    }

    appModel.findAll(condition)
            .then((apps)=>{
                console.log("Get: " + JSON.stringify(apps))
                res.status(200).json({result: 'success', desc: '', data: apps})
            })
            .catch((err)=>{
                res.status(500).json({result: 'fail', desc: err})
            })
})

/**
 * @swagger
 * /api/v2/app:
 *  post:
 *      summary:
 *      tags: [Application]
 *      description: 添加新的应用信息
 *      requestBody:
 *          required: true
 *          content:
 *              application/json:
 *                  schema:
 *                      type: object
 *                      required:
 *                          - categoryId
 *                          - name
 *                      properties:
 *                          categoryId:
 *                              type: string
 *                          name:
 *                              type: string
 *                          thumbnail:
 *                              type: string
 *                          deployed:
 *                              type: integer
 *                              defalut: 0           
 *      responses:
 *          200:
 *              description: successed
 *              content:
 *                  application/json:
 *                      schema:
 *                          type: object
 *                          properties:
 *                              result:
 *                                  type: string
 *                              desc:
 *                                  type: string
 *                              data:
 *                                  type: object
 *                                  properties:
 *                                      id:
 *                                          type: string
 *                                      categoryId:
 *                                          type: string
 *                                      name:
 *                                          type: string
 *                                      thumbnail:
 *                                          type: string
 *                                      deployed:
 *                                          type: integer
 *                                          
 */
router.post('/app', (req, res)=>{
    console.log('receive json body ' + JSON.stringify(req.body))

    appModel.create(req.body)
                    .then((app)=>{
                        console.log('Add: ' + JSON.stringify(app))
                        res.status(200).json({result: 'success', desc: '', data: app})
                    })
                    .catch((err)=>{
                        res.status(500).json({result: 'fail', desc: err})
                    })
})

/**
 * @swagger
 * /api/v2/app:
 *      put:
 *          summary:
 *          tags: [Application]
 *          description: 修改
 *          parameters:
 *              - name: id
 *                description: 要修改的应用信息id
 *                in: query
 *                required: true
 *                type: string
 *          requestBody:
 *              required: true
 *              content:
 *                  application/json:
 *                  schema:
 *                      type: object
 *                      properties:
 *                          categoryId:
 *                              type: string
 *                          name:
 *                              type: string
 *                          thumbnail:
 *                              type: string
 *                          deployed:
 *                              type: integer
 *          responses:
 *              200:
 *                  description: connect successed.
 *                  content:
 *                      application/json:
 *                          schema:
 *                              type: object
 *                              properties:
 *                                  result:
 *                                      type: string
 *                                  desc:
 *                                      type: string
 *  
 */
router.put('/app', (req, res)=>{
    appModel.update(req.body, { where: {id: req.query.id}})
                    .then((app)=>{
                        console.log('Update: ' + JSON.stringify(app))
                        res.status(200).json({result: 'success', desc: '' })
                    })
                    .catch((err)=>{
                        res.status(500).json({result: 'fail', desc: err })
                    })
})

/**
 * @swagger
 * /api/v2/app:
 *      delete:
 *          summary:
 *          tags: [Application]
 *          description: 删除应用信息
 *          parameters:
 *              - name: id
 *                in: query
 *                required: true
 *          responses:
 *              200:
 *                  description: deleted
 *                  content:
 *                      application/json:
 *                          schema:
 *                              type: object
 *                              properties:
 *                                  result:
 *                                      type: string
 *                                  desc:
 *                                      type: string
 */
router.delete('/app', (req, res)=>{
    appModel.destroy({ where: {id: req.query.id}})
                    .then((app)=>{
                        console.log('Delete: ' + JSON.stringify(app))
                        res.status(200).json({result: 'success', desc: ''})
                    })
                    .catch((err)=>{
                        res.status(500).json({result: 'fail', desc: err})
                    })
})

module.exports = {
    init: function(sequelize) {
        appModel = createModel(sequelize)
    },
    api: function() {
        return router
    }
}